#include<stdio.h>
#include<string.h>

struct record
{   
	char userid[25];
	char userpass[25];
	int key;
}data[60];

struct hash
{
	char uid[25];
	char upass[25];
}table[60];


hashing(char id[25], char pass[25], int k)
{
	int i,j;	
	int h,r;
	int a=0;

//	printf("%d", a);

	h = k % 60;
	r=strcmp((table[h].uid), "*");	
	
//	printf("%d", a);

	if(r==0)
	{
		strcpy(table[h].uid,id);
		strcpy(table[h].upass,pass);
	}
	else
	{
		while(table[h].uid != "*");
		{
			h++;
			r=strcmp((table[h].uid), "*");	
			if(r==0)
			{
				strcpy(table[h].uid,id);
				strcpy(table[h].upass,pass);
			}
		}
	}
	
//	printf("%d", a);
	
}


void main()
{
	FILE *fopen(), *fp;
	int i,j,k,l;
	i=0;
	char c;
	fp = fopen("hash_database.txt","r");
	  
	while (c!= EOF)
	{	
		j=0;
		while (c != ' ')
		{	 		
			c=getc(fp);               
			if(c==EOF)
			{
				break;
			}
			data[i].userid[j]=c;
			j++;
   		}
           
           
		k=0;
		while(c != '\n')
		{
			c=getc(fp);
			if(c==EOF)
			{			
				break;
			}
			data[i].userpass[k]=c;
			k++;
		}

	i++;
	}

	fclose(fp);
	
	for (i=0;i<60;i++)
	{
		printf("%s", data[i].userid);
		printf("  ");
		printf("%s", data[i].userpass);
		printf("\n");   
	}

	k=0;	

	for (i=0;i<60;i++)
	{
//		l=len(data[i].userid);
		for(j=0;j<25;j++)
		{
			k = k + (int)data[i].userid[j];
		}
		data[i].key=k;
		k=0;
	}
	
	for (i=0;i<60;i++)
	{
		printf("%s", data[i].userid);
		printf("  ");
		printf("%s", data[i].userpass);
		printf("  ");
		printf("%d", data[i].key);
		printf("\n");   
	}

	for (i=0;i<60;i++)
	{
		strcpy(table[i].uid, "*");
	}

	for (i=0;i<60;i++)
	{
		hashing(data[i].userid, data[i].userpass, data[i].key);
	}

	for (i=0;i<60;i++)
	{
		printf("%s", table[i].uid);
		printf("  ");
		printf("%s", table[i].upass);
		printf("\n");
	}

}
